package com.dragon.www.haier.util;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.Display;

import com.dragon.www.haier.SHApplication;
import com.dragon.www.haier.entity.User;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * SharedPreferences配置信息
 * 
 */
public class Config {

	// 用户信息
	private static String USER_INFO = "USER_INFO";

	// 房间ID
	private static String ROOM_ID = "roomid";

	// 房间名
	private static String ROOM_NAME = "roomname";

	// 房间描述
	private static String ROOM_DESC = "roomdesc";

	// 房间背景
	private static String ROOM_BG = "roombg";

	// 屏幕宽度
	private static String SCREEN_WIDTH = "screenwidth";

	// 屏幕高度
	private static String SCREEN_HEIGHT = "screenheight";


	public static SharedPreferences UserInfoPreferences = SHApplication.getContext().getSharedPreferences(USER_INFO, 0);

	public static int getRoomId() {
		return UserInfoPreferences.getInt(ROOM_ID, -1);
	}

	public static void setRoomId(int roomId) {
		UserInfoPreferences.edit().putInt(ROOM_ID, roomId).commit();
	}

	public static String getRoomName() {
		return  UserInfoPreferences.getString(ROOM_NAME, "");
	}

	public static void setRoomName(String roomName) {
		UserInfoPreferences.edit().putString(ROOM_NAME, roomName).commit();
	}

	public static String getRoomDesc() {
		return UserInfoPreferences.getString(ROOM_DESC, "");
	}

	public static void setRoomDesc(String roomDesc) {
		UserInfoPreferences.edit().putString(ROOM_DESC, roomDesc).commit();
	}

	public static String getRoomBg() {
		return UserInfoPreferences.getString(ROOM_BG, "");
	}

	public static void setRoomBg(String roomBg) {
		UserInfoPreferences.edit().putString(ROOM_BG, roomBg).commit();
	}

	public static User mUser  = new User();

	private static String USER = "User";

	public static void setUserInfo(User user) {
		try {
			String userInfoStr =  serialize(user);
			UserInfoPreferences.edit().putString(USER, userInfoStr).commit();
		}catch(Exception e){
			e.printStackTrace();
		}
		mUser = user;
	}

	public static User getUserInfo() {
		if(UserInfoPreferences.contains(USER)){
			try {
				String userInfoStr = UserInfoPreferences.getString(USER, "");
				mUser =  deSerialize(userInfoStr);
			}catch(Exception e){
				e.printStackTrace();
			}
		}

		return mUser ;
	}

	/**
	 * 序列化对象
	 *
	 * @return
	 * @throws IOException
	 */
	public static String serialize(User user) throws IOException {
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		ObjectOutputStream objectOutputStream = new ObjectOutputStream(
				byteArrayOutputStream);
		objectOutputStream.writeObject(user);
		String serStr = byteArrayOutputStream.toString("ISO-8859-1");
		serStr = java.net.URLEncoder.encode(serStr, "UTF-8");
		objectOutputStream.close();
		byteArrayOutputStream.close();
		return serStr;
	}

	/**
	 * 反序列化对象
	 *
	 * @param str
	 * @return
	 * @throws IOException
	 * @throws ClassNotFoundException
	 */
	public static User deSerialize(String str) throws IOException, ClassNotFoundException {
		String redStr = java.net.URLDecoder.decode(str, "UTF-8");
		ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
				redStr.getBytes("ISO-8859-1"));
		ObjectInputStream objectInputStream = new ObjectInputStream(
				byteArrayInputStream);
		User person = (User) objectInputStream.readObject();
		objectInputStream.close();
		byteArrayInputStream.close();
		return person;
	}

	public static int getScreenWidth() {
		return UserInfoPreferences.getInt(SCREEN_WIDTH, -1);
	}

	public static void setScreenWidth(int width) {
		UserInfoPreferences.edit().putInt(SCREEN_WIDTH, width).commit();
	}

	public static int getScreenHeight() {
		return UserInfoPreferences.getInt(SCREEN_HEIGHT, -1);
	}

	public static void setScreenHeight(int height) {
		UserInfoPreferences.edit().putInt(SCREEN_HEIGHT, height).commit();
	}

	/**
	 * 得到屏幕长宽
	 *
	 * @param activity
	 */
	public static void setScreenSize(Activity activity) {
		DisplayMetrics displayMetrics = new DisplayMetrics();
		Display display = activity.getWindowManager().getDefaultDisplay();
		display.getMetrics(displayMetrics);
		int width = displayMetrics.widthPixels;
		setScreenWidth(width);
		int height = displayMetrics.heightPixels;
		if(Build.VERSION.SDK_INT >= 14 && Build.VERSION.SDK_INT < 17) {
			try {
				height = (Integer) display.getClass()
						.getMethod("getRawHeight").invoke(display);

			} catch (Exception e) {
				e.printStackTrace();

			}
		}
		if(height == 0)
		{
			height = displayMetrics.heightPixels;
		}
		setScreenHeight(height);
	}
}
